package game.breeze.assets;

import game.breeze.ani.Animation;
import game.breeze.data.c.GameMap;
import game.breeze.util.XMLUtils;

import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Assets {
	public static Texture buttons;
	public static BitmapFont font;
	private static Map<Integer, GameMap> mapDic = new HashMap<Integer, GameMap>();
	public static Animation standAni;
	public static Animation moveAni;
	public static Animation attackAni;
	public static Animation cutAni;
	public static Animation attackedAni;
	public static Animation treeFall;
	public static TextureRegion treeInRiver;
	private static final int HERO_W = 100;
	private static final int HERO_H = 100;

	private Assets() {
	}

	public static void load() {
		font = new BitmapFont();
		Texture texture = new Texture(Gdx.files.internal("data/hero_motion.png"));
		standAni = new Animation(1.0f, 0, new TextureRegion(texture, 0, 0, HERO_W, HERO_H), new TextureRegion(texture, HERO_W, 0, HERO_W, HERO_H));

		TextureRegion[] m = new TextureRegion[5];
		for (int i = 0; i < m.length; i++) {
			m[i] = new TextureRegion(texture, i * HERO_W, 100, HERO_W, HERO_H);
		}
		moveAni = new Animation(.01f, 0, m);

		m = new TextureRegion[8];
		for (int i = 0; i < m.length; i++) {
			m[i] = new TextureRegion(texture, i * HERO_W, 200, HERO_W, HERO_H);
		}
		attackAni = new Animation(.2f, 0, m);

		m = new TextureRegion[4];
		for (int i = 0; i < m.length; i++) {
			m[i] = new TextureRegion(texture, i * HERO_W, 300, HERO_W, HERO_H);
		}
		cutAni = new Animation(.2f, 2, m);

		m = new TextureRegion[6];
		for (int i = 0; i < m.length; i++) {
			m[i] = new TextureRegion(texture, i * HERO_W, 400, HERO_W, HERO_H);
		}
		attackedAni = new Animation(.2f, 0, m);

		texture = new Texture(Gdx.files.internal("data/map/res10002/tree_fall.png"));
		m = new TextureRegion[17];
		for (int i = 0; i < m.length; i++) {
			m[i] = new TextureRegion(texture, i * 218, 0, 218, 239);
		}

		treeFall = new Animation(.2f, 0, m);

		texture = new Texture(Gdx.files.internal("data/map/res10002/tree_in_river.png"));
		treeInRiver = new TextureRegion(texture, 0, 0, 151, 66);
	}

	public static GameMap loadMap(int id) {
		if (mapDic.containsKey(id)) {
			return mapDic.get(id);
		}
		GameMap map = new GameMap(XMLUtils.toDocument(Gdx.files.internal("data/map/map_" + id + ".xml").read()));
		mapDic.put(id, map);
		return map;
	}

	public static void unload() {
		for (GameMap map : mapDic.values()) {
			map.dispose();
		}
		mapDic.clear();
	}
}
